<?php

namespace app\admin\controller;

use app\admin\model\Image;
use app\admin\model\Image_box;
use think\Controller;
use think\Db;
use think\Request;
use app\admin\controller\Base;

class Images extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        $rel = Db::name('image')->paginate(5,false,[
            'type'     => 'Bootstrap',
            'var_page' => 'page',
            'path'=>'javascript:AjaxPage([PAGE]);',
            //使用函数助手传入参数
            'query' => request()->param()
        ]);
        $imgbox = [];
        $imgbox = Db::name('image_box')->select();
        return view('image/imglist', ['data'=>$rel, 'imgbox'=>$imgbox]);
    }

    public function ajaxListAction(){

        $page = request()->param('apage');

        if (!empty($page)) {
            $rel = model('image')->paginate(5,false,[
                'type'=> 'Bootstrap',
                'var_page' => 'page',
                'page' => $page,
                'path'=>'javascript:AjaxPage([PAGE]);'

            ]);
            $page = $rel->render();
        }
        return json(['data'=>$rel,'page'=>$page]);

    }

    public function ajaxListSearch()
    {
        $name = input('post.')['name'];
        $data = Node::where('name', 'like', $name.'%')->paginate(6,false,[
            'type'     => 'Bootstrap',
            'var_page' => 'page',
            'path'=>'javascript:AjaxPage([PAGE]);',
            //使用函数助手传入参数
            'query' => request()->param()
        ]);

        return json(['data'=>$data]);
    }


    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $data = [];
        $data = Db::name('image_box')->select();


        return view('image/upimg',['data'=>$data]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function uploadImg(Request $request)
    {


        $info = input('post.');
        //var_dump($info);die();
        $box = Image_box::get($info['imgid']);

        $file = request()->file('photo');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $res = $file->move(ROOT_PATH . 'public/static/upload/photo');
            if($res){
                $info['imgpath'] = $res->getSaveName();
                $info['up_time'] = time();
                Image::create($info);
                $box->count ++;
                $box->save();
                $this->success('上传成功', url('admin/index/index'));
            }else{
                $this->success('上传失败,请上传图片格式', url('admin/index/index'));
            }
        }else{
            $this->success('上传失败,请选择图片', url('admin/index/index'));
        }
    }



    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $info = input('put.');
        $img = Image::get($id);
        $img->imgid = $info['imgbox'];
        $newbox = Image_box::get($info['imgbox']);
        $newbox->count ++;
        $newbox->save();
        $oldbox = Image_box::get($info['oldimgid']);
        $oldbox->count --;
        $oldbox->save();
        $result = $img->save();
        $data = [];
        if ($result>0){
            $data['status'] = true;
            $data['message'] = '修改成功';
        }else{
            $data['status'] = false;
            $data['message'] = '修改失败';
        }
        return json($data);

    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $img = Image::get($id);
        $box = Image_box::get($img->imgid);
        $box->count --;
        $box->save();
        $result = $img->delete();
        $data = [];
        if ($result>0){
            $data['status'] = true;
            $data['message'] = '修改成功';
        }else{
            $data['status'] = false;
            $data['message'] = '修改失败';
        }
        return json($data);



    }
}
